package com.ejlchina.tool.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.adolf.androiddemo.R
import com.adolf.androiddemo.databinding.FragmentWeightCalibrationBinding
import com.huyuhui.fastble.data.BleDevice
import com.ejlchina.tool.adapter.DeviceAdapterRadio
import com.ejlchina.tool.adapter.PushRodParametersAdapter2
import com.ejlchina.tool.adapter.pageadapter.ConfigFragmentPagerAdapter
import com.ejlchina.tool.adapter.pageadapter.WeightCalibrationPagerAdapter
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.enums.SuccessFailEnum
import com.ejlchina.tool.fragment.viewpager.ConfigViewPagerFragment
import com.ejlchina.tool.fragment.viewpager.WeightCalibrationPagerFragment
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.singleton.GlobalParams
import com.ejlchina.tool.util.COMMAND_CURRENT_WEIGHT_GET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_CURRENT_WEIGHT_GET_UPWARD
import com.ejlchina.tool.util.COMMAND_WEIGHT_CALIBRATION_DOWNWARD
import com.ejlchina.tool.util.COMMAND_WEIGHT_CALIBRATION_UPWARD
import com.ejlchina.tool.util.bytearray.intTo32HexStringLE
import com.ejlchina.tool.util.bytearray.intToByteHexString
import com.ejlchina.tool.util.bytearray.readByteArrayBE
import com.ejlchina.tool.util.bytearray.readInt32LE
import com.ejlchina.tool.view.GridItemDecoration
import com.ejlchina.tool.viewmodel.MainViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class WeightCalibrationFragment: BaseBindingFragment2<FragmentWeightCalibrationBinding>() {

    private val fragmentList = mutableListOf<WeightCalibrationPagerFragment>()
    private var pageAdapter :WeightCalibrationPagerAdapter? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    private fun initViews(){
        setHeadTitle("称重标定")
        setRightIconVisibility(View.GONE)
        initTabAndViewPager()
    }

    private fun initTabAndViewPager(){
        binding.tabLayout.tabMode = TabLayout.MODE_SCROLLABLE
        fragmentList.clear()
        fragmentList.addAll(GlobalConnectObject.getCurrentConnectDeviceList().map { WeightCalibrationPagerFragment(it) })

        pageAdapter = WeightCalibrationPagerAdapter(this,fragmentList)
        binding.viewPager.adapter = pageAdapter
        binding.viewPager.offscreenPageLimit = 1
        TabLayoutMediator(
            binding.tabLayout, binding.viewPager
        ) { tab, position -> tab.setText("${GlobalConnectObject.getCurrentConnectDeviceList()[position].bucketNo}#") }.attach()
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
    }



    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentWeightCalibrationBinding {
        return FragmentWeightCalibrationBinding.inflate(inflater,container,true)
    }
}